﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Windows.Forms;
using PicStore.BLL.Register;
using PicStore.UploaderInterface;
using WebMatrix.WebData;
using PicStore.BLL;
using PicStore.BLL.Impl;
using PicStore.BLL.Interface;
using PicStore.Common.Ioc;
using PicStore.Common.Log;
using PicStore.Model;
using log4net;

namespace PicStore.Client
{
    public partial class Login : Form
    {
        public Login()
        {
            var container = UnityContainerInstance.Instance;
            Bootstrapper.ConfigureUnity(container);
            ServiceRegistry.Register(container);

            InitializeComponent();
        }

        private void txtLogin_Click(object sender, EventArgs e)
        {
            PicStore.Common.Log.ILog log = ObjectFactory.GetInstance<PicStore.Common.Log.ILog>();

            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);
            var server = (IUploader)Activator.GetObject(typeof(IUploader), ConfigurationManager.AppSettings["RemotingUrl"]);
            try
            {
                bool isActive = server.IsServerActive();
            }
            catch (Exception exception)
            {
                MessageBoxButtons messButton = MessageBoxButtons.OK;
                DialogResult dr = MessageBox.Show("Remote server is not active, please contact administrator!", "Quit", messButton);
                log.Severe("login failed.", exception);
                return;
            }

            string userName = txtUserName.Text;
            string password = txtPassword.Text;

            var service = ObjectFactory.GetInstance<IUserService>();
            var user = service.GetUser(txtUserName.Text);
            if (user.IsDeleted)
            {
                lblUser.Text = "User is already deleted, login failed.";
                return;
            }
            else if (user.Role.Name == RoleEnum.Viewer.ToString())
            {
                lblUser.Text = "User don't have privilege to upload pictures。";
                return;
            }
            else
            {
                bool result = service.ValidateUser(txtUserName.Text, txtPassword.Text);
                if (result == false)
                {
                    lblUser.Text = "Password is invalid。";
                    return;
                }

                this.Hide();
                Upload uploader = new Upload(user.UserId);
                uploader.ShowDialog();
            }
        }

        private void txtCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtLogin_Click(sender, e);
            }
        }
    }
}
